package com.yoofn.kotlin.av.view.activity

import android.view.View
import com.yanzhenjie.nohttp.rest.Response
import com.yoofn.kotlin.R
import com.yoofn.kotlin.av.adapter.HomePagerAdapter
import com.yoofn.kotlin.av.view.fragment.HomeFragment
import com.yoofn.kotlin.av.view.fragment.MineFragment
import com.yoofn.kotlin.av.view.fragment.SettingFragment
import com.yoofn.kotlin.base.BaseActivity
import com.yoofn.kotlin.base.GlobalConfig
import com.yoofn.kotlin.base.ViewConfig
import com.yoofn.kotlin.databinding.ActivityMainBinding
import q.rorbin.badgeview.Badge
import q.rorbin.badgeview.QBadgeView


class MainActivity : BaseActivity<ActivityMainBinding, String>() {

    val itemId = intArrayOf(R.id.item_0, R.id.item_1, R.id.item_2, R.id.item_3)


    override fun setupConfig(): ViewConfig<String> {
        return ViewConfig(R.layout.activity_main)
    }

    val homeFragments by lazy {
        listOf(HomeFragment(), HomeFragment(), MineFragment(), SettingFragment())
    }

    override fun doInteractive() {
        with(contentView) {


            //++++++++++主页viewpager
            vpHome.run {
                if (GlobalConfig.COULD_SCROLL_HORIZONTAL) {
                    visibility = View.VISIBLE
                    adapter = HomePagerAdapter(supportFragmentManager, homeFragments)
                } else {
                    visibility = View.INVISIBLE
                    val beginTransaction = supportFragmentManager.beginTransaction()
                    beginTransaction.replace(R.id.fl_fragment, homeFragments[0], homeFragments[0]::class.simpleName)
                            .commit()
                }
            }


            //++++++++++底部导航
            bnvHome.run {
                addBadgeAt(2, 5)
                enableAnimation(false)
                enableShiftingMode(false)
                enableItemShiftingMode(false)
                if (GlobalConfig.COULD_SCROLL_HORIZONTAL) {
                    setupWithViewPager(vpHome)
                }
                setOnNavigationItemSelectedListener { menuItem ->
                    val position = itemId.indexOf(menuItem.itemId)
                    if (!GlobalConfig.COULD_SCROLL_HORIZONTAL) {
                        val beginTransaction = supportFragmentManager.beginTransaction()
                        beginTransaction.replace(R.id.fl_fragment, homeFragments[position], homeFragments[position]::class.simpleName)
                                .commit()
                    }
                    true
                }
            }


        }
    }

    override fun <ResponseType> doBusiness(what: Int, response: Response<ResponseType>) {

    }

    private fun addBadgeAt(position: Int, number: Int): Badge {
        // add badge
        return QBadgeView(this)
                .setBadgeNumber(number)
                .setGravityOffset(12f, 2f, true)
                .bindTarget(contentView.bnvHome.getBottomNavigationItemView(position))
                .setOnDragStateChangedListener { dragState, badge, targetView ->
                    //移除小气泡的监听

                }
    }

}




